package com.wtwd.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicParameters;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.model.entity.Device;
import com.wtwd.device.model.entity.UpgradeVo;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.AppDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * @author mjy
 * @date 2021/08/11
 * @description
 */
@Api(tags = "app设备模块")
@RestController
@RequestMapping("/app-device")
public class AppDeviceController {

    private static final Logger logger = LoggerFactory.getLogger(AppDeviceController.class);
    @Autowired
    private AppDeviceService appDeviceService;


    /**
     * 分页获取用户绑定设备列表
     *
     * @param currentPage
     * @param pageSize
     * @param userId
     * @return
     */
    @ApiOperation(value = "分页获取用户绑定设备列表", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage", value = "当前页", required = true, dataType = "int"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小", required = true, dataType = "int"),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "long")
    })
    @GetMapping("/getUserDeviceList")
    public RespEntity<Device> getUserDeviceList(@RequestParam("currentPage") Integer currentPage, @RequestParam("pageSize") Integer pageSize, @RequestParam("userId") Long userId) {
        logger.info("getUserDeviceList param" + userId);
        return appDeviceService.getUserDeviceList(currentPage, pageSize, userId);
    }


    /**
     * 用户绑定设备
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "用户绑定设备")
    @DynamicParameters(name = "userBindDevice", properties = {
            @DynamicParameter(name = "userId", value = "用户id", dataTypeClass = long.class, required = true)
            , @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "name", value = "设备名称", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "uuId", value = "设备广播uuId", dataTypeClass = String.class, required = false)
    })
    @PostMapping("/userBindDevice")
    public RespEntity userBindDevice(@RequestBody JSONObject jsonObject) {
        logger.info("userBindDevice param" + jsonObject);
        Long userId = jsonObject.getLong("userId");
        String uuId = jsonObject.getString("uuId");
        String macAddress = jsonObject.getString("macAddress");
        String name = jsonObject.getString("name");
        if (userId==null||StringUtils.isBlank(macAddress)){
            //测试连接
            return new RespEntity (RespCode.PARAMS_ERROR);
        }
        return appDeviceService.userBindDevice(userId, macAddress,name,uuId);
    }


    /**
     * 移除设备(解绑用户)
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "移除设备(解绑用户)")
    @DynamicParameters(name = "unbindDevice", properties = {
            @DynamicParameter(name = "userId", value = "用户id", dataTypeClass = long.class, required = true)
            , @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/unbindDevice")
    public RespEntity unbindDevice(@RequestBody JSONObject jsonObject) {
        logger.info("unbindDevice param" + jsonObject);
        Long userId = jsonObject.getLong("userId");
        String macAddress = jsonObject.getString("macAddress");
        return appDeviceService.unbindDevice(userId, macAddress);
    }


    /**
     * 移除设备(移除并且删除个人信息---暂未开发完成--运动数据暂无记录)
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "移除并且删除个人信息")
    @DynamicParameters(name = "unbindDeviceAndClearUserInfo", properties = {
            @DynamicParameter(name = "userId", value = "用户id", dataTypeClass = long.class, required = true)
            , @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/unbindDeviceAndClearUserInfo")
    public RespEntity unbindDeviceAndClearUserInfo(@RequestBody JSONObject jsonObject) {
        logger.info("unbindDevice param" + jsonObject);
        Long userId = jsonObject.getLong("userId");
        String macAddress = jsonObject.getString("macAddress");
        return appDeviceService.unbindDeviceAndClearUserInfo(userId, macAddress);
    }



    /**
     * 获取设备信息
     *
     * @param macAddress
     * @return
     */
    @ApiOperation(value = "获取设备信息", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "macAddress", value = "macAddress", required = true, dataTypeClass = String.class)
    })
    @GetMapping("/getDeviceInfo/{macAddress}")
    public RespEntity<Device> getDeviceInfo(@PathVariable("macAddress") String macAddress) {
        logger.info("getDeviceInfo param" + macAddress);
        return appDeviceService.getWatchByMacAddress(macAddress);
    }


    /**
     * 检测更新
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "检测更新")
    @DynamicParameters(name = "checkForUpdate", properties = {
            @DynamicParameter(name = "watchId", value = "手表id", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "currentFirmware", value = "当前版本", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "language", value = "语言", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/checkForUpdate")
    public RespEntity<UpgradeVo> checkForUpdate(@RequestBody JSONObject jsonObject) {
        logger.info("checkForUpdate param" + jsonObject);
        String watchId = jsonObject.getString("watchId");
        String currentFirmware = jsonObject.getString("currentFirmware");
        String language = jsonObject.getString("language");
        String macAddress = jsonObject.getString("macAddress");
        return appDeviceService.checkForUpdate(watchId, currentFirmware, language, macAddress);
    }


    /**
     * 更新设备完成之后的操作接口
     * 1.升级人数++
     * 2.修改设备当前版本
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation(value = "更新设备完成之后的操作接口")
    @DynamicParameters(name = "checkForUpdate", properties = {
            @DynamicParameter(name = "id", value = "升级包主键--检测更新时返回的id", dataTypeClass = Long.class, required = true)
            , @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "targetFirmware", value = "更新之后的版本", dataTypeClass = String.class, required = true)
    })
    @PostMapping("/updateDevice")
    public RespEntity updateDevice(@RequestBody JSONObject jsonObject) {
        logger.info("updateDevice param" + jsonObject);
        String targetFirmware = jsonObject.getString("targetFirmware");
        String macAddress = jsonObject.getString("macAddress");
        Integer id = jsonObject.getInteger("id");
        return appDeviceService.updateDevice(id, targetFirmware, macAddress);
    }


    /**
     * 通过macAddress,激活设备并上传手表id,和当前手表版本
     *
     * @param jsonObject
     * @return
     */
    @ApiOperation("激活设备")
    @DynamicParameters(name = "activationDevice", properties = {
            @DynamicParameter(name = "macAddress", value = "mac地址", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "watchId", value = "手表id", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "lng", value = "经度", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "lat", value = "纬度", dataTypeClass = String.class, required = true)
            , @DynamicParameter(name = "area", value = "国家区域", dataTypeClass = String.class, required = true)
    })
    @PutMapping("/activationDevice")
    public RespEntity activationDevice(@RequestBody JSONObject jsonObject) {
        logger.info("activationDevice" + jsonObject);
        String macAddress = jsonObject.getString("macAddress");
        String watchId = jsonObject.getString("watchId");
        String lng = jsonObject.getString("lng");
        String lat = jsonObject.getString("lat");
        String area = jsonObject.getString("area");
        if (StringUtils.isBlank(macAddress)||StringUtils.isBlank(watchId)||StringUtils.isBlank(lng)||StringUtils.isBlank(lat)||StringUtils.isBlank(area)){
            return new RespEntity(RespCode.PARAMS_ERROR);
        }
        return appDeviceService.activeDevice(macAddress, watchId,new Date().getTime(),1,lng,lat,area);
    }
}
